package com.gs.SynAntiWandering.db.dao;

import com.gs.SynAntiWandering.db.domain.HttpCache;
import com.gs.SynAntiWandering.db.domain.HttpCacheDao;

import java.util.List;

public class HttpCacheUtil {
    private static volatile HttpCacheUtil mInstance;
    private CommonDaoUtil<HttpCache> mHttpCacheUtil;

    public static HttpCacheUtil getInstance() {
        if (mInstance == null) {
            synchronized (HttpCacheUtil.class) {
                if (mInstance == null) {
                    mInstance = new HttpCacheUtil();
                }
            }
        }
        return mInstance;
    }

    public HttpCacheUtil() {
        HttpCacheDao httpCacheDao = DBUtil.getInstance().getWriteDaoSession().getHttpCacheDao();
        mHttpCacheUtil = new CommonDaoUtil<>(HttpCache.class, httpCacheDao);
    }

    public long insertOrReplaceCache(HttpCache httpCache) {
        return mHttpCacheUtil.insertOrReplace(httpCache);
    }

    public String getHttpCache(String url, String params) {
        List<HttpCache> httpCaches = mHttpCacheUtil.queryWhere(HttpCacheDao.Properties.Url.eq(url), HttpCacheDao.Properties.Param.eq(params));
        if (httpCaches != null && httpCaches.size() > 0) {
            return httpCaches.get(httpCaches.size() - 1).getResponse();
        }
        return "";
    }

    public long getCacheCount() {
        return mHttpCacheUtil.getCount();
    }
}
